package com.xiaozhi.libraryfunction.controller;

import com.xiaozhi.common.annotation.Log;
import com.xiaozhi.common.constant.UserConstants;
import com.xiaozhi.common.core.controller.BaseController;
import com.xiaozhi.common.core.domain.AjaxResult;
import com.xiaozhi.common.core.page.TableDataInfo;
import com.xiaozhi.common.enums.BusinessType;
import com.xiaozhi.common.utils.poi.ExcelUtil;
import com.xiaozhi.libraryfunction.domain.LibraryBook;
import com.xiaozhi.libraryfunction.domain.LibraryType;
import com.xiaozhi.libraryfunction.service.LibraryBookService;
import com.xiaozhi.libraryfunction.service.LibraryTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 书籍信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/admin/library/book")
public class LibraryBookController extends BaseController
{
    private String prefix = "library/book";

    @Autowired
    private LibraryBookService libraryBookService;
    @Autowired
    private LibraryTypeService libraryTypeService;

    @RequiresPermissions("library:book:view")
    @GetMapping()
    public String librarybook(ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        return prefix + "/book";
    }

    @RequiresPermissions("library:book:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LibraryBook book)
    {
        startPage();
        List<LibraryBook> list = libraryBookService.selectLibraryBookList(book);
        return getDataTable(list);
    }
    @Log(title = "书籍管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("library:book:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LibraryBook book)
    {
        List<LibraryBook> list = libraryBookService.selectLibraryBookList(book);
        ExcelUtil<LibraryBook> util = new ExcelUtil<LibraryBook>(LibraryBook.class);
        return util.exportExcel(list, "图书书籍");
    }

    /**
     * 新增书籍
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        return prefix + "/add";
    }

    /**
     * 新增保存书籍
     */
    @RequiresPermissions("library:book:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addtypeSave(@Validated LibraryBook book)
    {
        if (UserConstants.LibraryBook_NAME_NOT_UNIQUE.equals(libraryBookService.checkLibraryBookNameUnique(book)))
        {
            return error("新增书籍'" + book.getName() + "'失败，书籍名称已存在");
        }
        return toAjax(libraryBookService.insertLibraryBook(book));

    }

    /**
     * 修改书籍
     */
    @RequiresPermissions("library:book:edit")
    @GetMapping("/edit/{bookId}")
    public String typeedit(@PathVariable("bookId") Long bookId, ModelMap mmap)
    {
        mmap.put("libraryTypes",libraryTypeService.selectLibraryTypeList(new LibraryType()));
        mmap.put("librarybook", libraryBookService.selectLibraryBookById(bookId));
        return prefix + "/edit";
    }

    /**
     * 修改保存书籍
     */
    @RequiresPermissions("library:book:edit")
    @Log(title = "书籍管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LibraryBook book)
    {
        return toAjax(libraryBookService.updateLibraryBook(book));
    }

    @RequiresPermissions("library:book:remove")
    @Log(title = "书籍管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(libraryBookService.deleteLibraryBookByIds(ids));
    }
}
